/* Formating function for row details */
function fnFormatDetails ( oTable, nTr )
{
    var aData = oTable.fnGetData( nTr );
    var sOut = '<table cellpadding="1" cellspacing="0" border="0"  style="padding-left:20px;" class="bold">';
    sOut += '<tr><td><span><h5>Address</h5></span></td><td></td></tr>';
    sOut += '<tr><td>Door no.:</td><td>'+aData[16]+'</td></tr>';
    sOut += '<tr><td>Street:</td><td>'+aData[17]+'</td></tr>';
    sOut += '<tr><td>City:</td><td>'+aData[18]+'</td></tr>';
    sOut += '<tr><td>State:</td><td>'+aData[19]+'</td></tr>';
    sOut += '<tr><td>Country:</td><td>'+aData[20]+'</td></tr>';
    sOut += '<tr><td>Pin code:</td><td>'+aData[21]+'</td></tr>';
    sOut += '<tr><td>Phone no.:</td><td>'+aData[22]+'</td></tr>';
    sOut += '</table>';
     
    return sOut;
}


$(document).ready( function () {
	
    /*
     * Insert a 'details' column to the table
     */
    var nCloneTh = document.createElement( 'th' );
    var nCloneTd = document.createElement( 'td' );
    nCloneTd.innerHTML = '<img src="views/images/datatable/details_open.png">';
    nCloneTd.className = "center4";
     
    $('#employee_data_table_id thead tr').each( function () {
        this.insertBefore( nCloneTh, this.childNodes[0] );
    } );
     
    $('#employee_data_table_id tbody tr').each( function () {
        this.insertBefore(  nCloneTd.cloneNode( true ), this.childNodes[0] );
    } );
     

	
	
	
				var oTable = $('#employee_data_table_id').dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "full_numbers",
					"sDom": '<"H"CTflr>t<"F"ip>',
					"bStateSave": true,
					"iDisplayLength": 25,
				    "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]], 
				    "oLanguage": {
			            "sSearch": "Search all columns:"
			        },
			        "aoColumnDefs": [ 
			                         	{ "bSortable": false, "aTargets": [ 0 ] },
			                         	{ "bVisible": false, "aTargets": [ 3 ] },
			                         	{ "bVisible": false, "aTargets": [ 6 ] },
			                         	{ "bVisible": false, "aTargets": [ 7 ] },
			                         	{ "bVisible": false, "aTargets": [ 8 ] },
			                         	{ "bVisible": false, "aTargets": [ 9 ] },
			                         	{ "bVisible": false, "aTargets": [ 14 ] },
			                         	{ "bVisible": false, "aTargets": [ 16 ] },
			                         	{ "bVisible": false, "aTargets": [ 17 ] },
			                         	{ "bVisible": false, "aTargets": [ 18 ] },
			                         	{ "bVisible": false, "aTargets": [ 19 ] },
			                         	{ "bVisible": false, "aTargets": [ 20 ] },
			                         	{ "bVisible": false, "aTargets": [ 21 ] },
			                         	{ "bVisible": false, "aTargets": [ 22 ] },
			                           { "sClass": "centerCust", "aTargets": [ 5 ] }
			                       ],
	                                    
					"oTableTools": {
						"aButtons": [
							/*"copy", "csv", "xls", "pdf","print",*/
							
							{
								"sExtends": "copy",
								"sButtonText": "Copy to clipboard"
							},
							/*{
								"sExtends": "csv",
								"sButtonText": "CSV"
							},*/
							{
								"sExtends": "xls",
								"sButtonText": "Excel"
							},
							{
								"sExtends": "pdf",
								"sButtonText": "PDF"
							},
							{
								"sExtends": "print",
								"sButtonText": "Print View"
							}
														/*,"xls",
							{
								"sExtends":    "collection",
								"sButtonText": "Save",
								"aButtons":    [ "csv", "xls", "pdf" ]
							}*/
						],
						"oColVis": {
							"activate": "mouseover"
						},
				        "aaSorting": [[1, 'asc']]
					}
				} );
				
			    /* Add event listener for opening and closing details
			     * Note that the indicator for showing which row is open is not controlled by DataTables,
			     * rather it is done here
			     */
			   
				 $('#employee_data_table_id tbody td img').live('click', function () {
				        var nTr = $(this).parents('tr')[0];
				        if ( oTable.fnIsOpen(nTr) )
				        {
				            /* This row is already open - close it */
				            this.src = "views/images/datatable/details_open.png";
				            oTable.fnClose( nTr );
				        }
				        else
				        {
				            /* Open this row */
				            this.src = "views/images/datatable/details_close.png";
				            oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
				        }
				    } );
				 
				 
				$("#employee_data_table_id tr").hover(
						  function () {
						    $(this).css("background","#CCCC99").css("cursor","pointer");
						  }, 
						  function () {
						    $(this).css("background","");
						  }
						);
				 $("select").change(function(){
					 $("#employee_data_table_id tr").hover(
							  function () {
							    $(this).css("background","#CCCC99").css("cursor","pointer");
							  }, 
							  function () {
							    $(this).css("background","");
							  }
							);
				    });
				 

					 
					 $("a[rel^='prettyPopin']:eq(0)").prettyPopin({width: 750,followScroll:false});
					 $("a[rel^='prettyPopin']:eq(1)").prettyPopin({width: 550,followScroll:false});
					
			} );
			
			$(function() {
				$( "input:submit, a, input:button", ".demo" ).button();
				
			});
			 
				
				function rowSelect(obj) {
					
					 if(!($(obj).hasClass("select_edit"))){
						 $("#edit").attr("href","");
					 $("tr.select_edit").attr("style","background: !important");
					 $("tr.select_edit").removeClass("select_edit");
					 $(obj).attr("style","background:#94B52C!important");
					 $(obj).addClass("select_edit");
					 $("#edit span").text("Edit");
					 $("#edit").attr("href","EmpRegistrationAction.do?id="+$("tr.select_edit span.hide_").html()+"&pType="+$("#edit span").html());
					 }else{
						 $("tr.select_edit").attr("style","background: !important");
						 $("tr.select_edit").removeClass("select_edit");
						 $("#edit").attr("href","EmpRegistrationAction.do?pType=New");
						 $("#edit span").text("New");
					 }
				 }
				
				function deleteRec(){
						try{
								if($("tr.select_edit span.hide_").html()!=null){
									if(confirm("Do you really want to delete record ?")){
										$("form").attr("action", "EmpRegAction.html?id="+$("tr.select_edit span.hide_").html()+"&pType=Delete");
										$("form").submit();
									}
								}else{alert("Please select a record!!!");}
							}catch(e){
								
							}
						
				}
				
